package cn.cjx.basic.constant;

import cn.cjx.basic.domain.LoginData;
import cn.cjx.basic.utils.JwtUtils;
import cn.cjx.basic.utils.Payload;
import cn.cjx.basic.utils.RsaUtils;
import cn.cjx.user.domain.Logininfo;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * @author: cjx
 * @Date: 2022/5/17
 */
public class LoginContext {



	public static Logininfo getLoginData(HttpServletRequest request) {
		//1.从请求头获取JWT
		String jwtStr = request.getHeader("U-TOKEN");
		//2.空校验
		if (StringUtils.isEmpty(jwtStr)) {
			return null;
		}
		//使用公钥解密
		try {
			PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
			Payload<LoginData> payload = JwtUtils.getInfoFromToken(jwtStr, publicKey, LoginData.class);
			LoginData loginData = (LoginData) payload.getUserInfo();
			return loginData.getLogininfo();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
